/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-14 17:38:59
 * @Description  : 题目收藏接口的验证中间件
 */
const Joi = require('joi')
const { Op } = require('sequelize')
const R = require('@/models/Response')
const { QuestionFav } = require('@/models/model')
const { ERROR_USER_CODE, ERROR_SYSTEM_CODE } = require('@/constants/errorCode')
const { IS_INTEGER, IS_UNSAFE_INTEGER } = require('@/constants/joiSchema')
const { dumpCheck: globalDumpCheck } = require('@/utils/dumpCheck')

const questionFavSchema = {
  questionFavIds: Joi.array().items(IS_UNSAFE_INTEGER),
  questionFavId: IS_UNSAFE_INTEGER,
  page: IS_INTEGER.min(1).default(1),
  pageSize: IS_INTEGER.min(1).default(999),
  paperId: IS_UNSAFE_INTEGER,
  examId: IS_UNSAFE_INTEGER,
  questionId: IS_UNSAFE_INTEGER
}

const dumpCheck = {
  /**
   * @author: Hejh(3010733382@qq.com)
   * @description: 验证题目收藏ID的合法性
   * @param {array<string>} questionFavIds 题目收藏ID数组
   */
  checkQuestionFavIds: async (questionFavIds) => {
    let newquestionFavIds = [...new Set(questionFavIds)]
    // 不能出现重复的
    if (newquestionFavIds.length !== questionFavIds.length) {
      return false
    }

    newquestionFavIds = newquestionFavIds.map((item) => BigInt(item))

    if (newquestionFavIds.length === 0) {
      return true
    }

    // 查看不重复的和数据库的是否对的上
    const myWhere = {
      id: {
        [Op.in]: newquestionFavIds
      }
    }
    const questionFavs = await QuestionFav.findAll({
      where: myWhere
    })
    if (questionFavs.length !== newquestionFavIds.length) {
      return false
    }

    return true
  }
}

/**
 * @description 题目收藏列表查询的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.listQuestionFavValidate = async (ctx, next) => {
  const schema = Joi.object({
    page: questionFavSchema.page,
    pageSize: questionFavSchema.pageSize,
    paperId: questionFavSchema.paperId,
    userId: questionFavSchema.paperId,
    examId: questionFavSchema.examId
  }).validate(ctx.query)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }
  ctx.query = schema.value
  await next()
}

/**
 * @description 题目收藏列表查询的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.listPaperFavValidate = async (ctx, next) => {
  const schema = Joi.object({
    page: questionFavSchema.page,
    pageSize: questionFavSchema.pageSize,
    examId: questionFavSchema.examId.required()
  }).validate(ctx.query)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }
  ctx.query = schema.value
  await next()
}

/**
 * @description 创建题目收藏时的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.createQuestionFavValidate = async (ctx, next) => {
  const schema = Joi.object({
    questionId: questionFavSchema.questionId.required()
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { questionId } = ctx.request.body

  // 验证题目ID的合法性
  const b = await globalDumpCheck.checkQuestionIds([questionId])
  if (!b) {
    ctx.body = R.error(ERROR_USER_CODE.QUESTION_ID_ERROR)
    return
  }

  // 到这里说明参数没有问题,协作题目收藏情况[0],[-1],[xxx,xxx]
  await next()
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 批量删除题目收藏接口的参数验证
 * @param {*} ctx
 * @param {*} next
 */
module.exports.delQuestionFavsValidate = async (ctx, next) => {
  const schema = Joi.object({
    questionFavIds: questionFavSchema.questionFavIds.required()
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { questionFavIds } = ctx.request.body

  // 验证题目收藏ID数组的合法性
  const b4 = await dumpCheck.checkQuestionFavIds(questionFavIds)
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.QUESTION_FAV_ID_ERROR)
    return
  }
  await next()
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 题目收藏排序接口的参数验证
 * @param {*} ctx
 * @param {*} next
 */
module.exports.sortQuestionFavsValidate = async (ctx, next) => {
  const schema = Joi.object({
    questionFavIds: questionFavSchema.questionFavIds.required()
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { questionFavIds } = ctx.request.body

  // 验证题目收藏ID数组的合法性
  const b4 = await dumpCheck.checkQuestionFavIds(questionFavIds, true)
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.QUESTION_FAV_ID_ERROR)
    return
  }

  await next()
}
